﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using Maticsoft.DBUtility;
using System.Data;

namespace DMZS.DAL
{
    /// <summary>
    /// 通用类
    /// </summary>
    public class Common
    {
        /// <summary>
        /// 获取新单据编号
        /// </summary>
        /// <param name="orderType"></param>
        /// <returns></returns>
        public string GetCode(Model.DlyndxOrder.OrderType orderType)
        {
            var vCode = string.Empty;
            switch (orderType)
            {
                case Model.DlyndxOrder.OrderType.CGDD:
                    vCode = "CGDD";
                    break;
                case Model.DlyndxOrder.OrderType.CPRK:
                    vCode = "CPRK";
                    break;
                case Model.DlyndxOrder.OrderType.FHD:
                    vCode = "FHD";
                    break;
                case Model.DlyndxOrder.OrderType.XOUT:
                    vCode = "XOUT";
                    break;
                case Model.DlyndxOrder.OrderType.XSDD:
                    vCode = "XSDD";
                    break;
                case Model.DlyndxOrder.OrderType.RKD:
                    vCode = "RKD";
                    break;
                case Model.DlyndxOrder.OrderType.CKD:
                    vCode = "CKD";
                    break;
            }

            var strSql = @"
declare @vchtype int
select @vchtype=vchtype from Vchtype where vCode=@vCode
declare @code varchar(100)
exec [GetICBillNo] @vchtype,@code output
select @code
";
            var parameters = new SqlParameter[] { new SqlParameter("@vCode", SqlDbType.VarChar, 30) };
            parameters[0].Value = vCode;

            var obj = DbHelperSQL.GetSingle(strSql, parameters);
            if (obj != null)
            {
                return obj.ToString();
            }
            else
            {
                return null;
            }
        }
      
        /// <summary>
        /// 获取产品条码标签的最小值
        /// -1:失败
        /// </summary>
        /// <param name="ProductCode"></param>
        /// <param name="Date"></param>
        /// <param name="Dateformat"></param>
        /// <param name="Separator"></param>
        /// <param name="LengthOfSerialNmuber"></param>
        /// <returns></returns>
        public int GetBeginBarcodeNumber(string ProductCode, DateTime Date, string Dateformat, string Separator, int LengthOfSerialNmuber)
        {
            SqlParameter[] parameters = new SqlParameter[]{
                new SqlParameter("@ProductCode", SqlDbType.NVarChar,50),
                new SqlParameter("@Date", SqlDbType.DateTime),
                new SqlParameter("@Dateformat", SqlDbType.NVarChar,50),
                new SqlParameter("@Separator", SqlDbType.NVarChar,50),
                new SqlParameter("@LengthOfSerialNumber", SqlDbType.Int,4),
                new SqlParameter("@BeginBarcodeNumber", SqlDbType.Int, 4)
            };
            parameters[0].Value = ProductCode;
            parameters[1].Value = Date;
            parameters[2].Value = Dateformat;
            parameters[3].Value = Separator;
            parameters[4].Value = LengthOfSerialNmuber;
            parameters[5].Direction = ParameterDirection.Output;


            DbHelperSQL.RunProcedure("pGetBeginBarcodeNumber", parameters);
            object obj = parameters[5].Value;
            if (obj != DBNull.Value) return Convert.ToInt32(obj);
            else return -1;
        }
    }
}
